
import { createStore } from 'vuex'
import { login, getInfo, loginPhone } from '~/api/manager'
import {router, addRoutes} from '~/router'
import { setToken, removeToken } from '~/composables/auth'
import createPersistedState from "vuex-persistedstate"
import { api_project_constant } from '~/api/ordersApi'


// 创建一个新的 store 实例
const store = createStore({
  state() {
    return {
      user: {},

      // 侧边宽度
      asideWidth: "250px",

      // 菜单
      menus: [],

      // 权限
      ruleNames: [],

      // 项目常量
      projectConstant: {},

      // 项目ID，用于在项目详情页使用
      projectId: null,

      // 课程ID，用于在课程课时页使用
      sourceId: null,

      // 试卷模板ID，用于在试卷试题页使用
      configId: null,

      // 试卷ID，用于在试卷试题页使用
      paperId: null,

      //船舶ID，用于在船舶详情页使用
      shipId: null,
    }
  },
  mutations: {
    SET_USERINFO(state, user) {
      state.user = user
    },

    // 展开/缩起侧边
    handleAsideWidth(state) {
      state.asideWidth = state.asideWidth == '250px' ? '64px' : '250px'
    },

    // 设置
    SET_MENUS(state, menus) {
      state.menus = menus
    },

    SET_RULENAMES(state, ruleNames) {
      state.ruleNames = ruleNames
    },

    // 项目常量
    SetProjectConstant(state, projectConstant) {
      state.projectConstant = projectConstant
    },

    // 项目ID
    SetProjectId(state, projectId) {
      state.projectId = projectId
    },

    // 课程ID
    SetCourseId(state, courseId) {
      state.courseId = courseId
    },

    // 试卷模版ID
    SetConfigId(state, configId) {
      state.configId = configId
    },

    // 试卷ID
    SetPaperId(state, paperId) {
      state.paperId = paperId
    },
    // 船舶ID
    SetShipId(state, shipId) {
      state.shipId = shipId
    },
  },

  actions: {

    //登录
    login({ commit }, { username, password }) {
      return new Promise((resolve, reject) => {
        //loginType写死为0；表示pc端task管理系统登录
        login(username, password, "0")
          .then(res => {
            // 存储token
            // setToken( res.data.token)
            setToken(res.token)
            commit("SET_USERINFO", res.user)
            commit("SET_MENUS", res.user.treeList)
            commit("SET_RULENAMES", res.user.roles)
            addRoutes(res.user.treeList)
            resolve(res)
          }).catch(err => {
            reject(err)
          })
      })
    },

    loginPhone({ commit }, { key, phoneCode }) {
      return new Promise((resolve, reject) => {
        loginPhone(key, phoneCode)
          .then(res => {
            // 存储token
            // setToken( res.data.token)
            setToken(res.token)
            commit("SET_USERINFO", res.user)
            commit("SET_MENUS", res.user.treeList)
            commit("SET_RULENAMES", res.user.roles)
            addRoutes(res.user.treeList)
            resolve(res)
          }).catch(err => {
            reject(err)
          })
      })
    },

    // 项目常量
    getProjectConstant({ commit }) {
      return new Promise((resolve, reject) => {
        api_project_constant()
          .then(res => {
            console.log('project constant', res)
            commit("SetProjectConstant", res.user.roles)
            resolve(res)
          }).catch(err => {
            reject(err)
          })
      })
    },

    // 获取当前登录用户信息
    getInfo({ commit }, payload) {
      return new Promise((resolve, reject) => {
        commit("SET_USERINFO", payload.user)
        commit("SET_MENUS", payload.user.treeList)
        commit("SET_RULENAMES", payload.user.roles)
        resolve(res)
      })
    },

    // 刷新菜单
    refreshMenu({ commit }, treeList) {
      commit("SET_MENUS", treeList)
      console.log('menus , ', treeList)
      addRoutes(treeList)
      
    },

    // 退出登录
    logout({ commit }) {
      removeToken()
      commit("SET_USERINFO", {})
    },

  },

  plugins: [createPersistedState({
    storage: window.sessionStorage,
    reducer(val) {
      // return { // 只储存state中的user 
      //   user: val.base
      // }
      return val
    }
  })]
})

export default store
